#include"map.h"
#include "torch.h"
#include "ui_torch.h"
#include<QPainter>
#include<QKeyEvent>
#include<QRandomGenerator>
#include<QPixmap>
#include<QtMath>
#include<QString>



Torch::Torch(QWidget *parent)
    : QMainWindow(parent)
    ,isstart(true),isover(false), ui(new Ui::Torch),lives(6),direction(1),num(0)

{
    ui->setupUi(this);
    this->setGeometry(400,200,1000,600);

}

Torch::~Torch()
{
    delete ui;
}
//生成障碍
void Torch::becreat(Block& bl){

    starty=QRandomGenerator::global()->bounded(10);
    starts=QRandomGenerator::global()->bounded(2);
    switch(starty){
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:bl.posi=1;

        if(starts==0){
            bl.block.setRect(360,180,30,30);
            bl.size=1;

            }

         if(starts==1){
            bl.block.setRect(360,160,20,40);
            bl.size=2;

            }
        break;
    case 6:
    case 5:
    case 7:
    case 8:
    case 9:bl.posi=2;
        if(starts==0){
            bl.block.setRect(360,260,30,30);
            bl.size=1;
            }

        if(starts==1){
            bl.block.setRect(360,240,20,40);
            bl.size=2;
            }
        break;
    default:
        bl.block.setRect(360,170,20,40);
        bl.size=2;

    }
}
//初始化人
void Torch::begin(){
    isvictory=false;
isstart=false;
    score=0;
    pump=1;
    gravitydown=18;
    gravityup=12;
    isjump=false;
    ten=2;
    temn=8;
    pnum1=ten;
    pnum2=2*ten;//ten表示上升或下落的刷新次数


    background=Map();//背景
    {
        blocks.resize(10);
        becreat(blocks[0]);
        num=1;
    }
    person.setRect(250,163,20,36);

    timer1=startTimer(33);
    timer2=startTimer(1200);

    //timer3=startTimer(10);
    blood.resize(lives);
    for(int i=0;i<lives;i++){
        blood[i]=QRect(80+25*(i+1),45,15,4);
    }

}
//键盘事件，控制角色的上下与跳跃
void Torch::keyPressEvent(QKeyEvent*event){
    QKeyEvent *key=(QKeyEvent*)event;
    if(key->key()==Qt::Key_W){direction=1;pnum1=pnum2=ten;isjump=false;temn=20;}
    if(key->key()==Qt::Key_S){direction=2;pnum1=pnum2=ten;isjump=false;temn=20;}
    if(key->key()==Qt::Key_K&&person.bottom()<200&&isjump==false){
        direction=3;isjump=true;
    }
}
void Torch::paintEvent(QPaintEvent*event){

    QPainter painter(this);


   //背景图片的定向移动
    painter.drawPixmap(background.cx1,0,background.map1);
    painter.drawPixmap(background.cx2,0,background.map2);

    painter.drawPixmap(0,200,this->width(),10,QPixmap(":/image/images/trace.png"));
    painter.drawPixmap(0,280,this->width(),10,QPixmap(":/image/images/trace.png"));
    if(isstart)
        begin();
    painter.setBrush(Qt::black);

    {
        painter.drawPixmap(person.left(),person.top(),person.width(),person.height(),QPixmap(":/image/images/runman"+QString::number(pump)+".png"));
        if(pump==8)pump=1;
          else pump++;
    }
//画出障碍物
    painter.setBrush(Qt::black);

    for(int h=0;h<num;h++){

int a= blocks[h].block.left();
int  b= blocks[h].block.top();
int  c= blocks[h].block.width();
int  d=blocks[h].block.height();
if(blocks[h].size==1){
            if(blocks[h].posi==1)
        painter.drawPixmap(a,b,c,d,QPixmap(":/image/images/hitWallT.png"));
        if(blocks[h].posi==2)
            painter.drawPixmap(a,b,c,d,QPixmap(":/image/images/hitwallB.png"));
}
        if(blocks[h].size==2){
        painter.drawPixmap(a,b,c,d/2,QPixmap(":/image/images/Spiked Ball.png"));
        painter.drawPixmap(a,b+d/2,c,d/2,QPixmap(":/image/images/Spiked Ball.png"));
        }
    }
   if(isover)
    {
        display="游戏结束";

        QFont font1("Courier",30);
        painter.setFont(font1);
        painter.setPen(Qt::black);
        painter.drawText(400,150,display);
        killTimer(timer1);
        killTimer(timer2);
    }
//生命值的显示
    QFont font2("Courier",15);
    painter.setFont(font2);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::white);
    painter.drawText(50,50,"HP:");
    painter.drawText(300,50,QString::number(lives));
    painter.setBrush(Qt::red);
    if(!isover)
    {
    painter.drawRects(&blood[0],blood.size());
    }
    painter.setFont(font2);
    painter.setPen(Qt::black);
    painter.drawText(50,100,"SCORE");
    painter.drawText(150,100,QString::number(score));
}
//画面的更新
void Torch::picturemove(){
    background.mapmove();
     //tab=tab->next;
    if(direction==1)person.setRect(250,163,20,36);
    if(direction==2)person.setRect(250,243,20,36);
    if(direction==3&&isjump==true){
    if(pnum1>0){jump();pnum1--;}
    if(pnum1==0&&temn>0){temn--;}
    if(pnum1==0&&pnum2>0&&temn==0){fall();pnum2--;}
    if(pnum1==0&&pnum2==0){pnum1=pnum2=ten;isjump=false;temn=8;}
    }



    for(int f=0;f<num;f++)
    {
        blocks[f].block.setLeft(blocks[f].block.left()-3);
        blocks[f].block.setRight(blocks[f].block.right()-3);
 //障碍物超过左边界后的生成与更新
        if(blocks[f].block.left()<=20){
            for(int m=f;m<num-1;m++){
                blocks[m]=blocks[m+1];
            }num-=1;
        }

    }
    if(ishit()){
        lives--;
        for(int m=*pos;m<num-1;m++){
            blocks[m]=blocks[m+1];
        }num-=1;
    }
    blood.resize(lives);
    ( lives>0)?isover=false:isover=true;

    update();
}
//判断角色与障碍是否相撞，通过判断两个矩形中心的竖直与横向距离与两者的长或宽之和比较
bool Torch::ishit(){
    for(int g=0;g<num;g++){
        int le=blocks[g].block.left();
        int to=blocks[g].block.top();
        int hei=blocks[g].block.height();
        int wi=blocks[g].block.width();
        if(abs(to+hei/2-(person.top()+person.height()/2))<(hei+person.height())/2)
        {if(abs(le+wi/2-(person.left()+person.width()/2))<(wi+person.width())/2)
            {
                pos=&g;  return true;
            }
        }
    }return false;
}
void Torch::timerEvent(QTimerEvent *event) {
    int tem=event->timerId();
    if(tem == timer2) {
        if(num<blocks.size()){
            becreat(blocks[num]);
            num++;
        }
        score+=2;
    }
    if(tem == timer1) {
        picturemove();
    }
}
void Torch::jump(){
            person.setTop(person.top()-gravityup);
            person.setBottom(person.bottom()-gravityup);
    }
void Torch::fall(){
            person.setTop(person.top()+gravitydown);
            person.setBottom(person.bottom()+gravitydown);
}
